package com.cmcc.aregister.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.DataCallback_get_other_orders;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.constant.HelpInc;
import com.cmcc.aregister.cs.model.DataUtilEntity;
import com.cmcc.aregister.cs.model.OrderModel;
import com.cmcc.aregister.cs.util.Utils;

public class OtherOrderInfoAdapter extends BaseAdapter {
    private Context context;
    public List<OrderModel> list;
    public boolean isEnd = true;
    DataCallback_get_other_orders inc;
    HelpInc helpInc;

    public OtherOrderInfoAdapter(Context context, List<OrderModel> list,
        boolean isEnd, DataCallback_get_other_orders inc, HelpInc helpInc) {
        this.context = context;
        this.list = list;
        this.isEnd = isEnd;
        this.inc = inc;
        this.helpInc = helpInc;
    }

    public int getCount() {
        int size = list.size();
        if (!isEnd) {
            return size + 1;
        }
        else {

        }

        return size;
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    OnClickListener mOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DataUtilEntity mData = (DataUtilEntity) v.getTag();
            if (mData.msg.equals(context.getString(R.string.again_order))) {// 再次预约
                helpInc.notifyUI_other(GlobalSetting.AGIAN_ORDER,
                    mData.position);
            }
            else {// 取消预约
                  // helpInc.notifyUI(GlobalSetting.CANCEL_ORDER,mData.position);
            }
        }
    };

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (position >= list.size()) {
            convertView = LayoutInflater.from(context).inflate(
                R.layout.list_wait, null);
            convertView.setTag(null);
            convertView.setEnabled(false);
            inc.next_updateData_orders();
            return convertView;
        }
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                R.layout.a_order_detail_other, null);
            holder.order_id = (TextView) convertView
                .findViewById(R.id.order_id);
            holder.order_status = (TextView) convertView
                .findViewById(R.id.order_status);
            holder.order_info = (TextView) convertView
                .findViewById(R.id.order_info);
            holder.order_btn = (Button) convertView
                .findViewById(R.id.order_btn_other);
            holder.order_btn.setOnClickListener(mOnClickListener);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        final OrderModel order = list.get(position);
        holder.order_id.setText(order.orderNo);
        holder.order_status.setText(Utils.getOtherStatus(order.orderStatus));
        holder.order_info.setText(Utils.getOtherDesc(order.orderDesc));
        if (!order.isFastReservation.equals("1")) {
            holder.order_btn.setEnabled(false);
        }
        else {
            holder.order_btn.setEnabled(true);
        }
        DataUtilEntity mData = new DataUtilEntity();
        mData.msg = context.getString(R.string.again_order);
        mData.position = position;
        holder.order_btn.setTag(mData);
        return convertView;
    }

    public class ViewHolder {
        TextView order_id;
        TextView order_status;
        TextView order_info;
        Button order_btn;
    }
}